<template>
	<div :style="{'display':is.button?'inline':''}">
	<el-upload  :style="{'padding':name?'0':'0 13px','float':name?'left':'left','margin-left':name?'0':'5px'}" :action="geshi == 'form'?'':actionurl"
	 :show-file-list="false"
	 :headers="head"
	 :data="item"
  :before-upload="beforeAvatarUpload"
   :on-success="chenggong" :on-error="shibai"
  >
		<el-button  :size="is.button?'small':'text'" >{{title?title:'导入'}}</el-button>

		<!-- headers="'Content-Type': 'multipart/form-data'" -->
	</el-upload>
	<a class="el-button--text el-button--small" style="margin-left:40px" target="_blank" v-if="name&&url&&xianshi" :href="url">查看</a>
	</div>
</template>

<script>
	export default {
		name: 'upload',
		props: [
		  'actionurl',
		  'geshi',
		  'xianshi',
		  'title',
		  'name',
      'isbutton',
      'index'
    ],
		  computed:{
			  head(){
			  	return{
					Authorization:localStorage.getItem('authorization')
				}
			  },
		  },
		  data(){
			  return{
				  url:'',
          loading:'',
          item:{},
          is:{
				      button:true
          }
			  }
		  },
      mounted(){
        if(this.isbutton == 'false'){
            this.is.button = false
        }
        this.item.itemCode = this.$route.query.id
      },
		methods: {
			chenggong(response, file, fileList) {
				if (response&&response.statusCode == "200") {
					this.url = response.data;
					this.$message({
						message: '上传成功',
						type: 'success'
					});
					var data = response
					data.name =this.name?this.name:''
          if(this.index||this.index==0){
            data.index = this.index
          }
					this.$emit("test",data)
				}else{
					 this.$message.error(response.msg);
				}
        this.loading.close()
			},
			shibai(err, file, fileList) {
				if(err){
					 this.$message.error(err.msg);
				}
          this.loading.close()
			},
			   beforeAvatarUpload(file) {
           this.loading = this.$loading({
                     lock: true,
                     text: '请稍等',
                     customClass:'jiazaizhong',
                     spinner: 'el-icon-loading',
                     background: 'rgba(255,255,255, 0.7)'
                   });
					 if(file.size / 1024 / 1024 > 5){
						this.$message({
							message: '文件不能大于5M',
							type: 'warning'
						});
						this.loading.close()
						return false
					 }
				   this.$emit("uploadq",file)
				   if(this.geshi == 'form'){
				   var aa = new FormData()
				   aa.append("file",file);
				   aa.append("itemCode",this.$route.query.id);
					   return  this.$post(this.actionurl,aa).then(r=>{
						this.$message({
						   	message: '上传成功',
						   	type: 'success'
						});
						if(this.index||this.index==0){
						  r.index = this.index
            }
						this.$emit("test",r)
					   })
					   return false
				   }
				//const isJPG = file.type === this.geshi;
				/* const isLt2M = file.size / 1024 / 1024 < 2; */

				/* if (!isJPG) {
				  this.$message.error('只能上传'+this.geshi+"格式的文件");
				} */
				/* if (!isLt2M) {
				  this.$message.error('上传头像图片大小不能超过 2MB!');
				} */
				//return isJPG/* && isLt2M */;
			  }


		}
	};
</script>

<style>
	.el-upload.el-upload--text{
		border-radius: 0;
	}
</style>
